package org.devocean.groupbuying.service.aspects;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrInputDocument;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.devocean.groupbuying.domain.Article;
import org.devocean.groupbuying.domain.Merchant;
import org.devocean.groupbuying.utils.RenderMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SolrMerchantsAspect {
	
	@Autowired
	private SolrServer solrServer;
	
	@AfterReturning(
		      pointcut = "execution(* org.devocean.groupbuying.service.MerchantService.addOrUpdateMerchant(..))",
		      returning= "result")
	public void afterAddOrUpdateMerchant(JoinPoint joinPoint, Object result) throws SolrServerException, IOException{
		Merchant merchant = (Merchant)result;
		SolrInputDocument document = new SolrInputDocument();
		Article article = merchant.getArticlesLocalization().getArticles().iterator().next();
		document.addField("articleID", article.getArticleID());
		document.addField("title", article.getTitle());
		document.addField("lang", article.getLang());
		document.addField("articlesLocalizationID", merchant.getArticlesLocalization().getArticlesLocalizationID());
		document.addField("merchantID", merchant.getMerchantID());
		solrServer.add(document);
		solrServer.commit();
	}
	
	
	@Before("execution(* org.devocean.groupbuying.service.MerchantService.search(..))")
	public void beforeSearch(JoinPoint joinPoint) throws SolrServerException, IOException{
		Object[] args = joinPoint.getArgs();
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		
		String searchText = args[0].toString();
		Map<String,Object> searchResult = (Map<String, Object>) args[1];
		
		SolrQuery query = new SolrQuery("title:"+searchText).addFilterQuery("merchantID:[1 TO *]");
		QueryResponse response = solrServer.query(query);
		for (SolrDocument result : response.getResults()) {
			Map<String, Object> data = new ConcurrentHashMap<String, Object>(); 
		    for (String field : result.getFieldNames()) {
		        System.out.println(field + ": " + result.getFieldValue(field));
		        data.put(field, result.getFieldValue(field));		        
		    }
		    list.add(data);
		    System.out.println();
		}
		long total = response.getResults().size();
		searchResult.putAll(RenderMapUtils.getMap(list,total)); 
	}
	
	@AfterReturning("execution(* org.devocean.groupbuying.service.MerchantService.removeMerchant(..))")
	public void AfterRemoveMerchant(JoinPoint joinPoint) throws SolrServerException, IOException{
		Object[] args = joinPoint.getArgs();
		Integer merchantID = args.length > 0 ? (Integer)args[0] : null;
		solrServer.deleteByQuery( "merchantID:"+merchantID );
		solrServer.commit();
		
	}


}
